using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_OutDebugBlock : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_OutDebugBlock
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton button_05 { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton button_01 { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton button_03 { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton button_02 { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton button_04 { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("button_05", out var __tbv0);
                this.button_05 = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("button_01", out var __tbv1);
                this.button_01 = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("button_03", out var __tbv2);
                this.button_03 = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("button_02", out var __tbv3);
                this.button_02 = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("button_04", out var __tbv4);
                this.button_04 = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields

        private List<int> records = ListPool<int>.Get();
        private readonly int[] keys = { 1, 2, 3, 4 };

        #endregion fields

        #region properties

        protected UIB_UIN_OutDebugBlock ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_OutDebugBlock();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            // var cavGroup = this.gameObject.GetOrAdd<CanvasGroup>();
            // cavGroup.alpha = Debug.isDebugBuild ? 1.0f : 0.01f;
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.button_01.onClick.AddListener(this.OnClickBtn1);
            this.ui.button_02.onClick.AddListener(this.OnClickBtn2);
            this.ui.button_03.onClick.AddListener(this.OnClickBtn3);
            this.ui.button_04.onClick.AddListener(this.OnClickBtn4);
            this.ui.button_05.onClick.AddListener(this.OnClickBtn5);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {
            if (records != null)
                ListPool<int>.Put(records);
            records = null;
        }

        #region methonds

        public new void Show()
        {
            base.Show();
        }

        private void OnClickBtn1()
        {
            records.Add(1);
        }
        private void OnClickBtn2()
        {
            records.Add(3);
        }
        private void OnClickBtn3()
        {
            records.Add(4);
        }
        private void OnClickBtn4()
        {
            records.Add(2);
        }
        private void OnClickBtn5()
        {
            var arrs = records.ToArray();
            records.Clear();
            if (!IsSureKeys(arrs))
                return;
            this.OpenUI<UIGmOutside>();
        }

        private bool IsSureKeys(int[] records)
        {
            if (records.Length != 4)
                return false;

            for (var i = 0; i < keys.Length; i++)
            {
                if (keys[i] != records[i])
                    return false;
            }
            return true;
        }
        #endregion methonds
    }
}
